﻿//-------------------------------------------------------------------------------------------------
// <copyright file="QuoteService.cs" company="Andrew Craig Keller">
//  Copyright (c) Andrew Craig Keller.  All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace StockQuoteServices
{
    using System;
    using System.Collections.Generic;
    using StockQuoteServices.Contracts;

    /// <summary>
    /// Provides functionality for retrieving stock information, quotes, and historical prices.
    /// </summary>
    public class QuoteService : IQuoteService
    {
        /// <summary>
        /// Gets basic data and the last traded price for the stock with the given ticker symbol.
        /// </summary>
        /// <param name="tickerSymbol">
        /// the ticker symbol of the stock for which data is to be retrieved
        /// </param>
        /// <returns>
        /// a <see cref="Stock"/> object populated with basic information and the last price at
        /// which the stock was traded
        /// </returns>
        public Stock GetStock(string tickerSymbol)
        {
            IStockDataProvider dataProvider = new YahooStockDataProvider();
            return dataProvider.GetStockInfo(tickerSymbol);
        }

        /// <summary>
        /// Gets basic data, the last traded price, and historical price data for the given date
        /// range for the stock with the given ticker symbol.
        /// </summary>
        /// <param name="tickerSymbol">
        /// the ticker symbol of the stock for which data is to be retrieved
        /// </param>
        /// <param name="priceRangeStart">
        /// the starting date of the period for which historical price data is to be retrieved
        /// </param>
        /// <param name="priceRangeEnd">
        /// the ending date of the period for which historical price data is to be retrieved
        /// </param>
        /// <returns>
        /// a <see cref="Stock"/> object populated with basic information, the last price at which
        /// the stock was traded, and historical prices for the given date range
        /// </returns>
        public Stock GetStockWithPriceHistory(
            string tickerSymbol,
            DateTime priceRangeStart,
            DateTime priceRangeEnd)
        {
            Stock stock = this.GetStock(tickerSymbol);
            stock.HistoricalPrices = this.GetHistoricalPrices(
                tickerSymbol,
                priceRangeStart,
                priceRangeEnd);

            return stock;
        }

        /// <summary>
        /// Gets historical price data for the given date range for the stock with the given ticker
        /// symbol.
        /// </summary>
        /// <param name="tickerSymbol">
        /// The ticker symbol of the stock for which data is to be retrieved
        /// </param>
        /// <param name="priceRangeStart">
        /// the starting date of the period for which historical price data is to be retrieved
        /// </param>
        /// <param name="priceRangeEnd">
        /// the ending date of the period for which historical price data is to be retrieved
        /// </param>
        /// <returns>
        /// an <see cref="IList{T}"/> of <see cref="StockHistoricalPrice"/> objects for the
        /// given date range
        /// </returns>
        public IList<StockHistoricalPrice> GetHistoricalPrices(
            string tickerSymbol,
            DateTime priceRangeStart,
            DateTime priceRangeEnd)
        {
            IStockDataProvider dataProvider = new YahooStockDataProvider();
            return dataProvider.GetStockPriceHistory(tickerSymbol, priceRangeStart, priceRangeEnd);
        }
    }
}
